/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:张熳
 * Date:2025-10-07
 * Time:16:50
 */

//单链表---不带头单向不循环链表
public class MySingleList implements ISingleList{
    //创建一个内部类ListNode，表示一个节点，这个节点中存放的是一个val值和下一个节点的地址，除此之外，需要一个构造方法，初始化val
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    //头节点
    public ListNode head;

    @Override
    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //测试打印
    public void createList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        //node5.next = null; 不需要再对node5中的next置null，因为next引用是一个引用类型，默认的初始值为null
        this.head = node1;
    }

    @Override
    public int size() {
        int len = 0;
        ListNode cur = head;
        while (cur != null) {
            len++;
            cur = cur.next;
        }
        return len;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void addFirst(int date) {
        ListNode node = new ListNode(date);
        node.next = head;//将node变成新的头节点
        head = node;//更新头节点
    }

    @Override
    public void addLast(int date) {
        ListNode node = new ListNode(date);
        //链表为空的情况
        if (head == null) {
            head = node;
            return;
        }
        //不为空
        ListNode cur = head;
        //找尾
        while (cur.next != null) { //这里是cur.next != null
            cur = cur.next;
        }
        //此时cur是链表的尾节点
        cur.next = node;//更新node为新的尾节点
    }

    private void checkIndex(int index) throws IndexIllegalException {
        if (index < 0 || index > size()) {
            throw new IndexIllegalException("index位置不合法");
        }
    }
    @Override
    public void addIndex(int index, int date) {
        try {
            checkIndex(index);
            //在第一个位置/0位置插入使用 头插法
            if (index == 0) {
                addFirst(date);
                return;
            }
            //在最后一个位置插入使用 尾插法
            if (index == size()) {
                addLast(date);
                return;
            }
            //在中间位置插入
            ListNode node = new ListNode(date);
            //找index位置的前一个位置
            ListNode cur = head;
            while (index-1 != 0) {
                cur = cur.next;
                index--;
            }
            node.next = cur.next;
            cur.next = node;
        }catch (IndexIllegalException e) {
            e.printStackTrace();
        }
    }

    //寻找key数据所在节点位置
    private ListNode findNodeOfKey(int key) {
        ListNode cur = head;
        while (cur.next != null) { //这里是cur.next != null
            if (cur.next.val == key) {
                return cur;//如果cur下一个节点的val等于key，就返回cur，即返回key节点的前一个位置
            }
            cur = cur.next;
        }
        return null;
    }
    @Override
    public void remove(int key) {
        if (head == null) {
            return;
        }
        ListNode cur = findNodeOfKey(key);
        if (cur == null) {
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }

    @Override
    public void removeAllKey(int key) {
        if (head == null) {
            return;
        }
        ListNode prev = head;
        ListNode cur = head.next;
        while (cur != null) {
            if (cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        //处理头节点是key的情况   ———— 必须在这个位置，否则会出现漏删的情况
        if (head.val == key) {
            head = head.next;
            //return; //不可以加return，否则删完一个就要返回了
        }
    }

    @Override
    public void clear() {
        ListNode cur = head;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = null;//断开节点间的引用
            cur = curNext;//移动指针到下一个节点
        }
        head = null;//清空头节点
    }
}
